﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeForing.Common;

namespace TimeForing.Models
{
    public class TimeforingRepository
    {
        //private TimeforingDataContext contex = new TimeforingDataContext();
        private TimeforingEntities contex = new TimeForing.Models.TimeforingEntities();

        public TimeforingEntities Contex
        {
            get { return contex; }
            set { contex = value; }
        }

        #region Project

        public IQueryable<Project> FindAllProjects()
        {
            return Contex.Project.AsQueryable();
        }

      
        public Project GetProject(int id)
        {
            return Contex.Project.FirstOrDefault(d => d.ProjectID == id);
        }

        public void AddProject(Project project)
        {
            Contex.AddToProject(project);
        }
        #endregion

        #region Code

        public IQueryable<Code> FindAllCodes()
        {
            return Contex.Code.AsQueryable();
        }

        public Code GetCode(int id)
        {
            return Contex.Code.FirstOrDefault(d => d.CodeID == id);
        }


        public void AddCode(Code c)
        {
            Contex.AddToCode(c);
        }

      

        #endregion

        #region User

        public List<User> FindAllUsers()
        {
            return Contex.User.ToList();
        }

        public User GetUser(int id)
        {
            return Contex.User.FirstOrDefault(d => d.UserID == id);
        }


        public void AddUser(User u)
        {
            Contex.AddToUser(u);
        }

    

        #endregion

        #region Sprint

        public IQueryable<Sprint> FindAllSprints()
        {
            return Contex.Sprint.AsQueryable();
        }

        public IQueryable<Sprint> FindAllActiveSprints()
        {
            return Contex.Sprint.Where( s=> s.Archived == false).Select( s=>s);
        }


        public Sprint GetSprint(int id)
        {
            return Contex.Sprint.FirstOrDefault(d => d.SprintID == id);
        }

        public void AddSprint(Sprint s)
        {
            Contex.AddToSprint(s);
        }

      
        public List<Task> GetTasksInSprint(int sprintId)
        {
            //var items = from task in contex.Task
            //            where task.SprintID == sprintId
            //            select task;

            Sprint sprint= Contex.Sprint.Where(s => s.SprintID == sprintId).Single ();

            return sprint.Task.ToList(); 
            //List<Task> tasks = new List<Task>();
            //foreach (var item in items)
            //{
            //    tasks.Add(item);
            //}

            //return tasks;

        }

        #endregion

        #region Tasks

        public IQueryable<Task> FindAllTasks()
        {
            return Contex.Task.AsQueryable();
            //return contex.Tasks;
        }

        public Task GetTask(int id)
        {
            return Contex.Task.FirstOrDefault(d => d.TaskID == id);
            return Contex.Task.SingleOrDefault<Task>(d => d.TaskID == id);
            //return contex.Tasks.SingleOrDefault(d => d.TaskID == id);
        }

        public void AddTask(Task t)
        {
            Contex.AddToTask(t);
            //contex.Tasks.InsertOnSubmit(t);
        }

        public List<Task> GetUserWorkedOnTasksForWeek(User user, int week, int year)
        {
            DateTime startDate = Util.StartDateInWeek(week, year);
            DateTime endDate = startDate.AddDays(6);
            
            List<Task> regs = user.HourReg.Where(h => h.Date >= startDate && h.Date <= endDate).Select(h => h.Task).ToList();
            return regs;
        }


        #endregion

        #region HourRegs
        public void AddHourReg(HourReg h)
        {
            Contex.AddToHourReg(h);
            //contex.HourRegs.InsertOnSubmit(h);
        }

        public IQueryable<HourReg> GetHourRegs()
        {
            return Contex.HourReg.AsQueryable();
        }


        #endregion

        #region Stories

        public IQueryable<Stories> FindAllStories()
        {
            return Contex.Stories.AsQueryable();
            //return contex.Stories;
        }

        public Stories GetStory(int id)
        {
            return Contex.Stories.FirstOrDefault(d => d.StoryID == id);
        }

        public void AddStory(Stories s)
        {
            Contex.AddToStories(s);
           // contex.Stories.InsertOnSubmit(s);
        }

        #endregion


        public void Save()
        {
            Contex.SaveChanges();
            //contex.SubmitChanges();
        }
    }
}
